<?php

/**
 * @Author: kendy
 * @Date:   2019-12-18 15:49:32
 * @Last Modified by:   jiayanting
 * @Last Modified time: 2020-04-03 10:05:37
 */
namespace app\hotel\model;

use think\Model;
use think\Db;
use think\Session;
use think\Request;
class OrderComplains extends Model
{
	/** 
	 * @Author   kendy
	 * @DateTime 2019-12-19T10:54:09+0800
	 * @return   [type]                   [description]
	 */
  public function getHotelOrderComplainList($Iscount = false){

		$shopId = (int)Session::get('hotel_id');   
		$where['oc.object_id'] = $shopId;
		$where['oc.order_type'] = 2;
		$where['ho.is_del'] = 0; 
		$rs = Db::name("order_complains")->alias('oc') 
		           ->field('oc.object_type,ho.order_id,oc.complain_status,oc.create_time,oc.complain_content,u.nickname,u.phone,oc.order_sn')
				   ->join('__USER__ u','oc.u_id=u.uid','left')
				   ->join('__HOTEL_ORDER__ ho','oc.order_sn=ho.order_sn','left')
				   ->where($where)
				   ->order('oc.complain_id desc')
				   ->select(); 
		if(count($rs)>0)
		{
			if($Iscount)
				return count($rs);  
			foreach ($rs as $key => $value) {
			 	 $rs[$key]['create_time'] = date('Y-m-d H:i:s',$value['create_time']);
			 } 
			return $rs;
		}
		return $rs;
		 
	}

   public function getOrdercomplainsInfo($where)
   {  
    	$shopId = (int)Session::get('hotel_id');    
		$rs = Db::name("order_complains")->alias('oc') 
		           ->field('oc.object_type,ho.order_id,oc.complain_annex,oc.complain_status,oc.create_time,oc.complain_content,u.nickname,u.phone,oc.order_sn')
				   ->join('__USER__ u','oc.u_id=u.uid','left')
				   ->join('__HOTEL_ORDER__ ho','oc.order_sn=ho.order_sn')
				   ->where($where)
				   ->order('oc.complain_id desc')
				   ->find();
		if(!empty($rs))
		{ 
			 	 $rs['create_time'] = date('Y-m-d H:i:s',$rs['create_time']);
			 	 $rs['complain_annex'] = explode(',', $rs['complain_annex']);
			 return $rs;
		}
		return $rs;
   }


 /**
  * [solved 商家处理投诉订单结果]
  * @Author   kendy
  * @DateTime 2019-12-19T10:53:52+0800
  * @return   [type]                   [description]
  */
	public function solved()
	{
		$post = Request::instance()->post();
		$shopId = (int)Session::get('hotel_id'); 
        $where = ['order_sn'=>(int)$post['order_sn'],'complain_status'=>(int)$post['complain_status']];
        $result = Db::name('order_complains')
        ->where($where)
        ->find();  
        if($result){ 
           return true;
        }
        else
        { 
        	$info = Db::name('order_complains')
			        ->where(['order_sn'=>(int)$post['order_sn']])
			        ->update(['complain_status'=>(int)$post['complain_status']]);  
			if($info !== false) 
            return true;
        } 
        return false;

	}
}